In [1]:
type(())
Out[1]:
In [2]:
", ".join(dir(()))
Out[2]:
In [3]:
t = tuple()
print(t)
In [4]:
string = "Hello o/"
lst = [1, 2, 3, 4.2, "this", "is", "a", "test", 4j+2]
t = (23.0, 42.0, "here")
print(tuple(string))
print(tuple(lst))
print(tuple(t))
In [5]:
control = (2)
single1 = (2,) # <--- the `,` makes the difference
single2 = 2,
print(type(control), type(single1), type(single2))
In [6]:
point = (1.2, 3.4, 5.6, 9.8)
# or
#point = 1.2, 3.4, 5.6, 9.8
In [7]:
point.count(3.4)
Out[7]:
In [8]:
point.count(0)
Out[8]:
In [9]:
point.index(5.6)
Out[9]:
In [10]:
#point.index(0)
#---------------------------------------------------------------------------
#ValueError Traceback (most recent call last)
#<ipython-input-7-50c1b2cd61a9> in <module>()
#----> 1 point.index(0)
#
#ValueError: tuple.index(x): x not in tuple
In [11]:
0 in point
Out[11]:
In [12]:
point
Out[12]:
In [13]:
point[2]
Out[13]:
In [14]:
point[1:3]
Out[14]:
In [15]:
for _ in point:
print(_)
In [16]:
#point[0] = 2.0
#---------------------------------------------------------------------------
#TypeError Traceback (most recent call last)
#<ipython-input-13-8ce6d93959b6> in <module>()
#----> 1 point[0] = 2.0
#
#TypeError: 'tuple' object does not support item assignment
In [17]:
print(point)
t = (2.0, ) + point[1:]
print(t)
In [18]:
list(point)
Out[18]:
In [19]:
another_point = (1, 2, 3, 4)
In [20]:
point + another_point
Out[20]:
In [21]:
point * 2
Out[21]:
In [22]:
s = point
id(s) == id(point)
Out[22]:
In [23]:
a = tuple([0, 1, 2])
b = tuple([0, 3, 4])
print(a < b) # because 1 < 3
a = (0, 1, 200000000)
print(a < b) # because 1 < 3
In [24]:
a = 23
b = 42
print(a, b)
a, b = b, a
print(a, b)
In [25]:
addr = 'monty@python.org'
uname, domain = addr.split('@')
print(uname)
print(domain)
In [26]:
def min_max(values):
return min(values), max(values)
first, last = min_max(list(range(100)))
print(first, last)
In [27]:
def print_all(*args): # gather
print(args)
print_all(1, 2.0, '3')
In [28]:
t = (7, 3)
#print(divmod(t))
#---------------------------------------------------------------------------
#TypeError Traceback (most recent call last)
#<ipython-input-32-c3fe26201aaf> in <module>()
# 1 t = (7, 3)
#----> 2 print(divmod(t))
#
#TypeError: divmod expected 2 arguments, got 1
print(divmod(*t)) # scatter
In [29]:
s = 'abc'
t = [1, 2, 3]
In [30]:
for pair in zip(s, t):
print(pair)
In [31]:
for letter, number in zip(s, t):
print(letter, number)
In [32]:
for index, element in enumerate('abc'):
print(index, element)
In [33]:
list(zip(s, t))
Out[33]:
In [34]:
list(enumerate('abc'))
Out[34]:
In [35]:
list(zip('Anne', 'Elk'))
Out[35]:
In [36]:
d = {'a':0, 'b':1, 'c':2}
for key, value in d.items():
print(key, value)
In [37]:
t = [('a', 0), ('c', 2), ('b', 1)]
d = dict(t)
print(d)
In [38]:
d = dict(zip('abc', range(3)))
print(d)
In [39]:
from collections import namedtuple
Car = namedtuple('Car', 'color mileage')
my_car = Car('red', 3812.4)
print(my_car.color)
print(my_car.mileage)
print(my_car)
In [40]:
try:
my_car.color = 'blue'
except AttributeError as _:
print(_)